package com.k.note;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.core.MainApp;

import java.util.Map;

/**
 * Created by mac_wk on 14-12-15.
 */
public class MainActivityProxy2 extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2);
        homeKeyEventBroadCastReceiver = new HomeKeyEventBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        intentFilter.addAction("action.com");
        registerReceiver(homeKeyEventBroadCastReceiver, intentFilter);
        MainApp mainApp = (MainApp) getApplication();
        for (Map.Entry<String,Activity> entry:mainApp.activityMap.entrySet())
        {
            Log.d("message",entry.getKey());
       //     entry.getValue().finish();
        }
        mainApp.activityMap.put("MainActivityProxy2",this);
        Log.d("message_size",mainApp.activityMap.size()+"");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(homeKeyEventBroadCastReceiver);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d("message","onNewIntent");
    }
    private HomeKeyEventBroadCastReceiver homeKeyEventBroadCastReceiver;
    class HomeKeyEventBroadCastReceiver extends BroadcastReceiver
    {
        static final String SYSTEM_REASON = "reason";
        static final String SYSTEM_HOME_KEY = "homekey";//home key
        static final String SYSTEM_RECENT_APPS = "recentapps";//long home key
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if ("action.com".equals(action))
            {
                Log.i("message", "key home is click!");
                finish();
            }
//            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
//            {
//                String reason = intent.getStringExtra(SYSTEM_REASON);
//                if (reason != null) {
//                    if (reason.equals(SYSTEM_HOME_KEY))
//                    {
//
//
//                        // home key处理点
//                        finish();
//
//                    } else if (reason.equals(SYSTEM_RECENT_APPS))
//                    {
//                        // long home key处理点
//                    }
//                }
//            }
        }
    }
}
